﻿using InstallWizard.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace InstallWizard.DAL
{
    internal class FileIO
    {
        public static AppInfo ReadConfigFile(string file, out List<AppFileInfo> appFileList)
        {
            AppInfo AppInfo = new AppInfo();

            appFileList = new List<AppFileInfo>();

            XmlDocument xdoc = new XmlDocument();
            xdoc.Load(file);

            XmlNodeList list = xdoc.SelectNodes("InstallConfig/AppInfo");
            if (list != null)
            {
                foreach (XmlNode n in list)
                {
                    AppInfo.AppName = n.Attributes["Name"].Value;
                    AppInfo.AppDesc = n.Attributes["Description"].Value;
                    AppInfo.AppVer = n.Attributes["Version"].Value;
                    AppInfo.DotnetVersion = n.Attributes["DotnetVersion"].Value;
                    AppInfo.AppLicenseFile = n.Attributes["LicenseFile"].Value;
                    AppInfo.AppLogoFileName = n.Attributes["LogoFile"].Value;
                    AppInfo.CompanyName = n.Attributes["CompanyName"].Value;
                    AppInfo.Size_MB = (float)Convert.ToDouble(n.Attributes["Size_MB"].Value);
                }
            }
            if (string.IsNullOrEmpty(AppInfo.CompanyName)) AppInfo.CompanyName = "CC SoftLab";
            list = xdoc.SelectNodes("InstallConfig/Files/File");
            if (list != null)
            {
                foreach (XmlNode n in list)
                {
                    AppFileInfo fileInfo = new AppFileInfo()
                    {
                        SourceFile = n.Attributes["SourceFile"].Value,
                        TargetDir = n.Attributes["TargetDir"].Value ?? "",
                        FileType = (EFileType)Enum.Parse(typeof(EFileType), n.Attributes["FileType"].Value),
                        Proceed = (EProceed)Enum.Parse(typeof(EProceed), n.Attributes["Proceed"].Value)
                    };
                    //if (string.IsNullOrEmpty(n.Attributes["TargetDir"].Value))
                    //    fileInfo.TargetDir = "";
                    //else
                    //    fileInfo.TargetDir = n.Attributes["TargetDir"].Value;
                    //fileInfo.Proceed = (EProceed)Enum.Parse(typeof(EProceed), n.Attributes["Proceed"].Value);
                    appFileList.Add(fileInfo);
                }
            }
            return AppInfo;
        }

    }
}
